package encore.portal.container.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.PortletURLGenerationListener;

import encore.log.Logger;
import encore.log.LoggerFactory;
import encore.portal.container.services.PortletURLListenerService;
import encore.portal.portlet.models.Listener;
import encore.portal.portlet.models.PortletApplicationDefinition;

/**
 * 该类的功能是:从PortletApplicationDefinition的Listeners获取值并创建相应的实例
 * @author Plex
 *
 */
public class PortletURLListenerServiceImpl implements PortletURLListenerService {

	private static final Logger logger=LoggerFactory.getLogger(PortletURLListenerServiceImpl.class);
	@Override
	public List<PortletURLGenerationListener> getPortletURLGenerationListeners(
			PortletApplicationDefinition applicationDefinition) {
		List<PortletURLGenerationListener> listerners=new ArrayList<PortletURLGenerationListener>();
	    List<? extends Listener> portletURLFilterList=applicationDefinition.getListeners();
	    if(portletURLFilterList!=null)
	    {
	     for(Listener listener:portletURLFilterList)
	     {
	    	 ClassLoader loader=Thread.currentThread().getContextClassLoader();
	    	 Class<? extends Object> clazz;
	    	 try{
	    		 clazz=loader.loadClass(listener.getListenerClass());
	    		 if(clazz!=null)
	    			 listerners.add((PortletURLGenerationListener)clazz.newInstance());
	    		 
	    	 }catch (ClassNotFoundException e) {
			    logger.error("The class isn't found.");
			} catch (InstantiationException e) {
				logger.error("The instantiation fail.");
			} catch (IllegalAccessException e) {
				logger.error("IllegalAccessException");
			}
	    	 
	     }
	    }
		return listerners;
	}

}
